﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QLNHASACH.DTO;
namespace QLNHASACH.DataAccessLayer.ObjectDAO
{
    class DauSachDAO
    {
        //Kiem tra dau sach da ton tai chua
        public static bool KiemTraTonTai(DauSachDTO dauSach)
        {
            string sql="Select MaDauSach,TenDauSach from DAUSACH where TenDauSach=N'" + dauSach.TenDauSach + "'";
            if (DataAccess.GetDataTable(sql).Rows.Count > 0)
                return true;
            return false;
        }
        public static int LayMaDauSachTheoTen(string ten)
        {
            string sql="select MaDauSach from DAUSACH where TenDauSach=N'" + ten + "'";
            int maDS = (int)DataAccess.GetDataTable(sql).Rows[0].ItemArray[0];
            return maDS;
        }
        public static bool ThemDauSach(DauSachDTO dauSach)
        {
            string sql =string.Format("INSERT INTO DAUSACH(TenDauSach,MaTheLoai,TacGia)"
                + "VALUES (N'{0}',{1},N'{2}')", dauSach.TenDauSach, dauSach.MaTheLoai, dauSach.TacGia);
            if (DataAccess.ExecuteQuery(sql))
                return true;
            return false;
        }
        //Update dau sach
        public static bool CapNhatDauSach(DauSachDTO dauSach)
        {
            string sql =string.Format("UPDATE DAUSACH Set TenDauSach=N'{0}',MaTheLoai={1},TacGia=N'{2}' where MaDauSach={3}"
               , dauSach.TenDauSach, dauSach.MaTheLoai, dauSach.TacGia, dauSach.MaDauSach);
            if (DataAccess.ExecuteQuery(sql))
                return true;
            return false; ;
        }
        public static bool XoaMotDauSach(DauSachDTO dauSach)
        {
            string sql="DELETE  FROM DAUSACH WHERE MaDauSach=" + dauSach.MaDauSach + "";
            if (DataAccess.ExecuteQuery(sql))
            {
                return true;
            }
            return false;
        }
    }
}
